
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  basicprocess.inheritance;

import java.util.Hashtable;
import java.util.LinkedList;
import java.util.Map;
import java.util.Vector;


import pool.tablename.Property_details;
import pool.tablename.Pool;
import utility.ConvertToLower;
import utility.Input;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;
import file.FileOperation;
import file.SearchRecord;

public class Il implements Inheritance
{
	DBManager database;
	Hashtable<Object, Object>Mastermap= new Hashtable<Object, Object>();
	Vector< Object>propertyVector = new Vector<Object>();
	GetIdName gid;
	String INHERITANCE="inheritance";
	String SCOPE="scope";
	String IL="il";
	Pool pool;
	SearchRecord sr;
	Vector<Object>finalvector = new Vector<Object>();

	public Vector<Object> getpropertydetailsVector() 
	{
		return this.finalvector;
	}


	public void processInheitance() 
	{	
		
		
	}
	
	public void getUpdate()
	{	
		Hashtable<Object,Object>Mastertable = new Hashtable<Object, Object>();
		Mastertable.putAll(pool.getHashtabel());
		String parentmasterid = gid.getId(Mastertable.get("name").toString());
		findchild(parentmasterid);
		
		for(int i=0;i<pool.getpropertyVector().size();i++)
		{
			Property_details pd = (Property_details) pool.getpropertyVector().get(i);
			pd.PropertyMap.putAll( gid.convertMasterName(pd.PropertyMap, "property_details"));
		}
	}
	
	public void findchild(String parentmasterid)
	{
		try 
		{
			String MasterPath = Input.MASTERREPORTSPATH +  INHERITANCE + "//" + INHERITANCE + "_pid" + Input.FILRFORMAT;
			sr = new SearchRecord(MasterPath);
			sr.fileOpen();
			Hashtable<Object, Object>temp = new  Hashtable<Object, Object>();
			UpdateChild(parentmasterid);
			temp.put("pv",parentmasterid);
			String result[] =sr.getRecordSet(temp);
			sr.fileClose();
			for(int t=0;t<result.length;t=t+1)
			{
				try 
				{
					String splitresult[] = result[t].split("\t");
					findchild(splitresult[Column.mid_index-1]);
				}
				catch (Exception e) 
				{	
					e.printStackTrace();
				}				
			}
			
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
	
	public void UpdateChild(String Child)
	{
		String ChildMasterReport = Input.MASTERREPORTSPATH + Child + "//" + Child+ "_mid" +Input.FILRFORMAT;
		Hashtable<Object, Object>temp = new  Hashtable<Object, Object>();
		FileOperation fileoperation = new FileOperation(ChildMasterReport);
		Hashtable<Object, Object>searchtable = new Hashtable<Object, Object>();
		for(int j=0;j<pool.getpropertyVector().size();j++)
		{
			Hashtable<Object, Object> property = new Hashtable<Object, Object>();
			property.putAll((Map<? extends Object, ? extends Object>) pool.getpropertyVector().get(j));
			temp.clear();
			try 
			{
				temp.put("mid", Child);
				temp.put("pid", property.get("pid"));
				if(property.containsKey("PV"))
				temp.put("PV", property.get("PV"));
				if(property.containsKey("VT"))
				temp.put("VT", property.get("VT"));
				if(property.containsKey("Ref"))
				temp.put("Ref", property.get("Ref"));
			}
			catch (Exception e) 
			{
				e.printStackTrace();
			}
			searchtable.put("mid", Child);
			searchtable.put("pid", temp.get("pid"));
			SearchRecord srecord = new SearchRecord(ChildMasterReport);
			srecord.fileOpen();
			long indeofline = srecord.getRecordIndex(searchtable);
			srecord.fileClose();
			fileoperation.updateRecord(indeofline, temp);
		}
	}
	
	public void getInsert()
	{
		Property_details pdobject = new Property_details();
		LinkedList<Object>  linkedlist = new LinkedList<Object>();
		for(int i=0;i<pool.getpropertyVector().size();i++)
		{
			Property_details pd = (Property_details)pool.getpropertyVector().get(i);
			if(!pd.PropertyMap.get("pid").equals(INHERITANCE))
			{
				linkedlist.add(pd.PropertyMap.get("pid"));
				//pdobject.PropertyMap.clear();
				pdobject.PropertyMap.putAll(pd.PropertyMap);
				finalvector.add(pdobject);
			}
		}
		for(int i=0;i<pool.getpropertyVector().size();i++)
		{
			Property_details pd = (Property_details)pool.getpropertyVector().get(i);
				try
				{	
						
					if(pd.PropertyMap.get("pid").equals(INHERITANCE))
					{
						String parentobject = pd.PropertyMap.get("pv").toString();
						Vector<Object> vector = new Vector<Object>();
						Hashtable<Object, Object>temptable = new Hashtable<Object, Object>();
						String path = Input.MASTERREPORTSPATH + parentobject +"//"+ parentobject + "_mid" + Input.FILRFORMAT;
						sr.fileOpen();
						sr = new SearchRecord(path);
						temptable.put("mid", parentobject);
						vector = sr.getVectorSet(temptable);
						sr.fileClose();
						basicprocess.inheritance.Scope scope = new basicprocess.inheritance.Scope ();
						scope.InitPool();
						vector = scope.checkforscope(vector,parentobject);
						
						System.out.println(pd.PropertyMap);
						finalvector.add(pd);
						for(int j=0;j<vector.size();j++)
						{
							temptable.clear();
							temptable = (Hashtable<Object, Object>) vector.get(j);
							temptable = ConvertToLower.convertHashKey(temptable);
							String propertyid =temptable.get("pid").toString();
							if(!linkedlist.contains(propertyid))
							{	
								pdobject = new Property_details();
								if(temptable.containsKey("mid"))
									temptable.remove("mid");
								pdobject.PropertyMap.putAll(temptable);
								finalvector.add(pdobject);
								System.out.println(pdobject.PropertyMap);
							}	
						}
						break;	//MARUTEE
					}
				}
				catch (Exception e) 
				{
					e.printStackTrace();
				}
		}
		pool.setPropertyVector(finalvector);
	}

	public void InitPool() 
	{	
		gid = new GetIdName();
		INHERITANCE = gid.getId(INHERITANCE);
		SCOPE = gid.getId(SCOPE);
		IL = gid.getId(IL);
	}
	
	public void setobject(Object object)
	{
		pool = (Pool) object;
	}
	
	/*@SuppressWarnings("unchecked")
	public Vector<Object> checkforscope(Vector<Object> vector,String ParentObject) 
	{
		LinkedList<Object> linkedlist = new LinkedList<Object>();
		for(int i=0;i<vector.size();i++)
		{
			Hashtable<Object, Object>table =(Hashtable<Object, Object>) vector.get(i);
			table = ConvertToLower.convertHashKey(table);
			//String splitvector[] = vector.get(i).toString().split("\t");
			
		
			if(table.get("pid").equals(SCOPE))
			{
				String scopvalue = table.get("pv").toString();
				if(!linkedlist.contains(scopvalue))
					linkedlist.add(scopvalue);
			}
		}
		for(int i=0;i<linkedlist.size();i++)
		{	 
			 try 
			 {
				 Scope scope;
				 Class C = Class.forName("scope."+CapitalChar.makeFirstCharCapital(gid.getItem(linkedlist.get(i).toString())));
				 scope  =(Scope)C.newInstance();
				 scope.setDbmanger(database);
				 scope.setVector(vector);
				 scope.getParent(ParentObject);
				 scope.processScope();
				 vector = scope.getVector();
			 }
			 catch (ClassNotFoundException e) 
			 {
				e.printStackTrace();
			 }
			 catch (InstantiationException e) 
			 {
				e.printStackTrace();
			 }
			 catch (IllegalAccessException e) 
			 {
				e.printStackTrace();
			 }
		}
		for(int i=0;i<vector.size();i++)
		{
			Hashtable<Object, Object>  table = new Hashtable<Object, Object>();
			table =(Hashtable<Object, Object>) vector.get(i);
			if(table.get("pid").equals(INHERITANCE))
				vector.remove(i);
		}
		return vector;
	}*/
	
}
